<?php
/**
 * Api接口层模块
 * User: henrick
 * Date: 2017/3/22
 * Time: 18:17
 */
namespace app\core;

use think\Controller;

class Apicontroller extends Controller
{
    protected $no_auth_url = [   //不用权限校验的url
        'apiv1/home/wxusercode',
        'apiv1/home/index'
    ];

    protected $platform = ['wechat_app','web','wechat_mp','app'];

    protected $login_no_auth_url = [  //登录不需要权限验证
        'admin/api/getqiniuuploadtoken',  //上传七牛云token
        'admin/api/delqiniures',          //七牛云资源删除接口
    ];

    protected $token = '';

    protected function _initialize(){
        //判断接口必填参数
        $req = request();
        $platform = $req->param('platform');
        if(!$platform || !in_array($platform,$this->platform)){
            $this->_err(1,'非法接入，缺省参数');
        }

        $module     = $req->module();
        $controller = $req->controller();
        $action     = $req->action();
        $url = strtolower($module.'/'.$controller.'/'.$action);

        if(in_array($url,$this->no_auth_url)){
            return true;
        }


        //判断接口是否需要验证接口token
        $token = $req->param('token');
        if(!$token){
            $this->_err(1,'缺少签名参数！');
        }

        $res = $this->_checkToken($token,$platform);
        if($res){
            return true;
        }

        $this->_err(1,'你还没有登录！');


        //$this->_routerAuth();
    }

    protected function _checkToken($token,$platform){ //验证平台token是否有效
        if($platform==$this->platform[0]){ //小程序token验证
            $session_key = cache($token);
        }

        if(isset($session_key['time']) && $session_key['time']<=time()){
            $this->_err(1,'token已超过有效期');
        }else if(!isset($session_key['time'])){
            $this->_err(1,'没有权限使用本接口');
        }

        $session_key['time'] = time()+7100;
        $this->token = $token;

        cache($token,$session_key);
        return true;
    }

    protected function _routerAuth(){  //权限校验
        $url = url('','',false);
        $url = ltrim(str_replace('/index.php/','',$url),'/');
        //echo $url;exit;
        //判断url是否是为不用权限校验
        if(in_array($url,$this->no_auth_url)){
            return true;
        }

        $admin_info = session('user_info');

        //判断是否登录
        $res = $this->_isLogin();
        if(!$res){
            $this->redirect(url('index/index/login'));
            exit;
        }

        if(in_array($url,$this->login_no_auth_url)){
            return true;
        }

        //判断是否有权限访问
        $auth_list = cache('cache_user_id_'.$admin_info['id']);
        if(in_array($url,$auth_list['user_auth'])){
            unset($auth_list,$admin_info);
            return true;
        }

        $this->redirect(url('index/index/page403'));
        exit;
    }

    protected function _isLogin(){   //是否登录
        $admin_info = session('user_info');
        if(!$admin_info){
            return false;
        }
        return true;
    }

    protected function show($path){
        $menu = $this->_getMenu();
        $this->assign('menu',$menu);
        echo $this->fetch('default/layout/header');
        echo $this->fetch('default/'.$path);
        echo $this->fetch('default/layout/footer');
    }

    protected function _getMenu(){  //获取菜单
        $menu = cache('menu');
        if($menu){
            return $menu;
        }
        $menu = model('menu');
        $menu = $menu->field('name,url')->select();

        cache('menu',$menu);
        return $menu;
    }

    protected function _err($code=0,$msg='成功',$data=[]){
        echo json_encode(['err'=>$code,'msg'=>$msg,'data'=>$data],JSON_UNESCAPED_UNICODE);
        exit;
    }
}